package com.jgy.handler;

import com.jgy.utils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler extends Throwable {

    //统一异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.failed();
    }

    //指定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.failed().setMsg("数字计算型错误！");
    }

    //自定义异常
    @ExceptionHandler(LightException.class)
    @ResponseBody
    public R error(LightException e){
        e.printStackTrace();
        return R.failed().setMsg(e.getMessage()).setCode(e.getCode());
    }
}
